package com.sparrow.config;

import com.sparrow.annotation.AnnotationHelper;
import com.sparrow.annotation.PackageScan;
import com.sparrow.annotation.bean.RequestUriInfo;
import com.sparrow.annotation.RequestUrlHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by wancheng on 2019/1/5.
 */
@Configuration
public class LogConfig {
    private final Logger LOG = LoggerFactory.getLogger(LogConfig.class);

    public static List<RequestUriInfo> requestMappingList = null;

    @Autowired
    PackageScan packageScan;

    @Bean
    public void logMethod() {
        LOG.info("系统初始化成功");
    }

    @Bean
    public void getAnnotationObject() {
        List<Class<?>> controllerSet = new ArrayList<>();
        List<RequestUriInfo> res = new ArrayList<>();
        try {
            controllerSet = packageScan.getControllerClass();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        for (Class<?> clz : controllerSet) {
            Annotation clzAnnotation = AnnotationHelper.getInstance().getClassAnnotation(clz, RequestMapping.class);
            RequestUriInfo controllerUri = null;
            try {
                List<RequestUriInfo> controllerUris = RequestUrlHelper.getInstance().getRequestUrlInfos(clzAnnotation);
                if (!CollectionUtils.isEmpty(controllerUris)) {
                    controllerUri = controllerUris.get(0);
                }
            } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                e.printStackTrace();
            }
            List<Annotation> requestMapperAnnotations = AnnotationHelper.getInstance().getMethodAnnotation(clz, RequestMapping.class);
            for (Annotation a : requestMapperAnnotations) {
                try {
                    List<RequestUriInfo> urlList = RequestUrlHelper.getInstance().getRequestUrlInfos(a);
                    RequestUriInfo finalControllerUri = controllerUri;
                    if (finalControllerUri != null) {
                        urlList.forEach(
                                u -> {
                                    u.setControllerMapping(finalControllerUri.getValue());
                                    u.setControllerName(finalControllerUri.getName());
                                }
                        );
                    }
                    res.addAll(urlList);
                } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }
        res.forEach(System.out::println);
        requestMappingList = res;
    }
}
